/*
 * 文件名: SingleSelectDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-24
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import app.widget.BaseDialog;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-24
 * @version [Android PABank C01, @2011-12-24]
 * @description
 */
public class SingleSelectDialog extends BaseDialog {

	/**
	 * ListView
	 */
	private ListView listView;
	
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 选项集合
	 */
	private List<Map<String,String>> list;
	/**
	 * 返回View
	 */
	private View contentView;
	/**
	 * 返回的位置
	 */
	private int mCurrentPosition;
	
	/**
	 * key值
	 */
	String key = null;
	
	/**
	 * value值
	 */
	String value = null;
	/**
	 * 
	 * [构造简要说明]
	 * @param context 上下文
	 * @param title Dialog的标题
	 * @param list 选项集合
	 * @param current_postion 当前默认的选项
	 */
	public SingleSelectDialog(Context context,String title,List<Map<String,String>> list ,int currentPosition) {
		
		super(context);
		this.context = context;
		
		
		this.list = list;
		mCurrentPosition = currentPosition;
		
		setTitle(title);
		
		/*Map<String,String> map = list.get(currentPosition);
		Set<Entry<String,String>> entries = map.entrySet();
		
		for(Map.Entry<String, String> entry : entries){
			key = entry.getKey();
			//value = entry.getValue();
		}
		
		Log.i("zbkc", "construct:"+key+"|"+value);*/
		
		
		initView();
	}
	
	public SingleSelectDialog(Context ctx, String title, String[] strings) {
	    super(ctx);
        this.context = ctx;
        list = new ArrayList<Map<String,String>>();
        for (int i =0; i<strings.length; i++) {
            
            Map<String,String> map = new HashMap<String, String>();
            map.put(String.valueOf(i), strings[i]);
            list.add(map);
        }
        
        setTitle(title);
        
        initView();
	}
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化view
	 */
	private void initView() {
		contentView = LayoutInflater.from(context).inflate(R.layout.app_widget_dialog_select, null);
		
		listView = (ListView)contentView.findViewById(R.id.list);
		
		
		listView.setDivider(null);
		listView.setCacheColorHint(0x00000000);
		
		
		listView.setVerticalScrollBarEnabled(false);
		
		listView.setAdapter(new DialogAdapter(context,list));
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCurrentPosition = position;
				Map<String,String> map = list.get(position);
				Set<Entry<String,String>> entries = map.entrySet();
				
					for(Map.Entry<String, String> entry : entries){
						key = entry.getKey();
					}
				
				
				
				if (onSelectEndListner != null ) {
				    onSelectEndListner.onSelectEnd(key, mCurrentPosition);
				}
				
				if (onItemSelectListener != null ) {
				    onItemSelectListener.onItemSelect(mCurrentPosition);
				}
				
				SingleSelectDialog.this.dismiss();
			}
			
		});
		
		setBtn1Visible(false);
		setBtn2Visible(false);
	}

	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2012-1-16]
	 */
	class DialogAdapter extends BaseAdapter {
		/**
		 * LayoutInfalter
		 */
		private LayoutInflater inflater;
		/**
		 * 选项集合
		 */
		private List<Map<String,String>> list;
		/**
		 * 缓存对象
		 */
		ViewHolder holder;
		
		/**
		 * 
		 * [构造简要说明]
		 * @param context 上下文
		 * @param list 集合
		 */
		public DialogAdapter(Context context,List<Map<String,String>> list) {
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			
			
			
			if (convertView == null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.app_widget_dialog_select_item, null);
				holder.textView = (TextView)convertView.findViewById(R.id.textView);
				convertView.setTag(holder);
				
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			
			
			
			//PABankLog.i("zbkc", "getView:position"+position+"");
			
			
			Map<String,String> map = list.get(position);
			Set<Entry<String,String>> entries = map.entrySet();
			
			for(Map.Entry<String, String> entry : entries){
				value = entry.getValue();
			}
			
			
			holder.textView.setText(value);
			
			
			
			
			
			return convertView;
		}
		
		class ViewHolder{
			TextView textView;
		}
		
	}
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 选择完成回调接口
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2011-12-26]
	 */
	public interface OnSelectEndListner {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * @param result 返回的结果
		 */
		void onSelectEnd(String key , int resultPosition);
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2012-1-16]
	 */
	public interface OnItemSelectListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * @param position 位置
		 */
	    public void onItemSelect(int position);
	}
	
	/**
	 *  回调监听
	 */
	private OnSelectEndListner onSelectEndListner;
	/**
	 * 回调监听
	 */
	private OnItemSelectListener onItemSelectListener;
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]getter
	 * @return OnSelectEndListner
	 */
	public OnSelectEndListner getOnSelectEndListner() {
		return onSelectEndListner;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * @param onSelectEndListner 回调监听器
	 */
	public void setOnSelectEndListner(OnSelectEndListner onSelectEndListner) {
		this.onSelectEndListner = onSelectEndListner;
	}
	@Override
	public View createContentView() {
		// TODO Auto-generated method stub
		return contentView;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param onItemSelectListener 单项选择监听
	 */
    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener)
    {
        this.onItemSelectListener = onItemSelectListener;
    }
    /**
     * 
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @return OnItemSelectListener
     */
    public OnItemSelectListener getOnItemSelectListener()
    {
        return onItemSelectListener;
    }
	

}

